package com.erp.module.pieceworkGrouping

import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import com.erp.Interface.OnUserDataInterface
import com.erp.bean.PieceworkcollBean
import com.erp.bean.ProductionExecuteBean
import com.erp.pull.BasePullP
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import com.erp.util.Sputil
import org.json.JSONArray
import org.json.JSONException
import org.json.JSONObject
import org.xutils.http.cookie.DbCookieStore

class PieceworkGroupingP(var V : PieceworkGroupingContract.PieceworkGroupingFragmentView, var context: Context, var mlist : MutableList<PieceworkcollBean>):BasePullP(), PieceworkGroupingContract.ProductionFragmentKP {
    val sp: SharedPreferences = Sputil.sp("erp");
    val kggdata = hashMapOf<String,String>();
    var md = PieceworkGroupingM()
    init {
        PullV=V
    }
    fun getGangweiGroupingId() :String{
        var kggId = sp.getString("gangwei_grouping_id", "");
        return kggId!!;
    }

    override fun getData(page: Int, pagesize: Int, onUserDataInterface: OnUserDataInterface) {
        var pages = page+1;
        md.getPieceworkcoll(context,pages,pagesize,onUserDataInterface)
    }

    override fun jsondata(data: String?) {
        try {
            var jo = JSONObject(data)
            var joo = JsonUtil.getjovolue(jo,"data")
            var js = JsonUtil.getjsvolue(joo,"data")
            for (i in 0 until js.length()){
                var jodata: JSONObject = js.get(i) as JSONObject
                var bean = PieceworkcollBean()
                bean.gangwei_name = JsonUtil.getvolue(jodata,"gangwei_name")
                bean.xm = JsonUtil.getvolue(jodata,"xm")
                bean.created_at = JsonUtil.getvolue(jodata,"created_at")
                bean.creator_name = JsonUtil.getvolue(jodata,"creator_name")
                var jsnames = JsonUtil.getjsvolue(jodata,"worker_group")
                var worker_group = "";
                for(j in 0 until jsnames.length()){
                    var joname: JSONObject = jsnames.get(j) as JSONObject
                    if(worker_group != ""){
                        worker_group += ","
                    }
                    worker_group += JsonUtil.getvolue(joname, "name")
                }
                bean.worker_group = worker_group
                bean.qr_content = JsonUtil.getvolue(jodata,"qr_content")
                bean.content_cat = JsonUtil.getvolue(jodata,"content_cat")
                var jsbatch = JsonUtil.getjsvolue(jodata,"batch_names")
                var batch_names = ""
                for(j in 0 until jsbatch.length()){
                    if(batch_names != ""){
                        batch_names += ","
                    }
                    batch_names += jsbatch.get(j)
                }
                bean.batch_names = batch_names
                bean.square = JsonUtil.getvolue(jodata,"square")
                bean.pcount = JsonUtil.getvolue(jodata,"pcount")
                bean.mcount = JsonUtil.getvolue(jodata,"mcount")
                mlist.add(bean)
            }

        }catch (e :JSONException){
            e.printStackTrace()
            return
        }
    }

    override fun listClean() {
        mlist.clear()
    }

    override fun getPiecework() {
        var kgg_id = getGangweiGroupingId();
        Log.d("onActivityResult","kgg_id="+kgg_id)
        if(kgg_id != ""){
            md.getPiecework(context,kgg_id,object : OnUserDataInterface{
                override fun onUserData(data: String?) {
                    pieceworkJson(data)
                    kggdata["gangwei_name"]?.let { V.getPieceworkOk(it) }
                }

                override fun onNot(throwable: String?) {
                }

            })
        }
    }

    fun pieceworkJson(data : String?){
        if(data==null){
            return
        }
        try {
            var jo = JSONObject(data)
            var joo = JsonUtil.getjovolue(jo,"data")
            kggdata["gangwei_name"] = JsonUtil.getvolue(joo,"gangwei_name")
            kggdata["gangwei_id"] = JsonUtil.getvolue(joo,"gangwei_id")
            kggdata["kgg_id"] = JsonUtil.getvolue(joo,"kgg_id")
            kggdata["xm"] = JsonUtil.getvolue(joo,"xm")
            var jsstaff = JsonUtil.getjsvolue(joo,"stafflist")
            val staffjson = JSONArray()
            for(j in 0 until jsstaff.length()){
                var jostaff: JSONObject = jsstaff.get(j) as JSONObject
                var jost = JSONObject()
                jost.put("uid",JsonUtil.getvolue(jostaff, "uid"))
                jost.put("coefficient",JsonUtil.getvolue(jostaff, "coefficient"))
                staffjson.put(jost)
            }
            kggdata["worker_group"] = staffjson.toString()
            kggdata["unit_price"] = JsonUtil.getvolue(joo,"unit_price")
        }catch (e : JSONException){
            Log.d("onSuccess",e.toString())
            e.printStackTrace()
            return
        }

        Log.d("projsondata",mlist.toString())
    }

    override fun postPieceworkcoll(code: String,ishuancun:Boolean) {
        var map = hashMapOf<String,String>();
        kggdata["gangwei_id"]?.let { map.put("gangwei_id", it) }
        kggdata["kgg_id"]?.let { map.put("kgg_id", it) }
        kggdata["xm"]?.let { map.put("xm", it) }
        kggdata["unit_price"]?.let { map.put("unit_price", it) }
        kggdata["worker_group"]?.let { map.put("worker_group", it) }
        kggdata["gangwei_name"]?.let { map.put("gangwei_name", it) }
        map.put("qr_content", code)
        md.postPieceworkcoll(context,map,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                V.postPieceworkcollOk(code,ishuancun)
            }

            override fun onNot(throwable: String?) {
                V.postPieceworkcollNo(code,ishuancun)
            }

        })
    }

    override fun postScanCodel(scan_code: String, sign: String) {
        md.caveScancode(context,scan_code,sign,object : OnUserDataInterface{
            override fun onUserData(data: String?) {
                Myutil.MyTost("扫描数据上传成功")
                V.postScanCodelOk()
            }

            override fun onNot(throwable: String?) {
                V.postScanCodelNo()
            }

        })
    }
}